--
-- @Author:      name
-- @DateTime:    20189-07-30 23:05:48
-- @Description: 所有游戏桌子共用到的消息

--local skynet = require "skynet"
--local config = require "configquery"
-- require "common"

-- local AssertEx = AssertEx


local DeskMessage = class("DeskMessage")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function DeskMessage:ctor(message_dispatch)
	self.room_config = nil --房间配置
	self:register()
end

--注册本服务里的消息
function DeskMessage:register()

    --self.message_dispatch:registerSelf('cotrolReq', handler(self,self.cotrolReq))

	self:callback("games.REQ_DESK_ENTER", self.reqEnterDesk)
	self:callback("games.REQ_DESK_LEAVE", self.reqLeaveDesk)
	self:callback("games.REQ_DESK_SITDOWN", self.reqSitDown)
	self:callback("games.REQ_DESK_SITUP", self.reqStandUp)


    g.dispatch:register('disconnect', handler(self,self.disconnect))
end







---------------------------------------------------------
-- CMD
---------------------------------------------------------
--服务初始化
--function DeskMessage:start(table_id)
--	print("____________game_start_________")
--	self.table_id = table_id
--	self.config = global.config
--	self.node_message = global.node_message
--	g.user_manager = global.user_manager
--	g.desk = global.desk
--	self.message_handler = global.message_handler
--
--
--	self.config:start(table_id) --房间配置
--	g.desk:start(table_id)
--	self.message_handler:start(table_id)
--end

--进入桌子
function DeskMessage:reqEnterDesk(data)
	print("____reqEnterDesk____", tostring(data))

    local user_id = data.user_id   
    local table_id = data.table_id 
    local user = g.user_manager:getUser(user_id)
    if not user then
		g.user_manager:addUser(user_id)
		user = g.user_manager:getUser(user_id)
    end
    user:setDisconnect(false)
     --在线人数+1
    if g.desk.enterDeskCallback then 
		g.desk:enterDeskCallback(user_id)
    end
	local res_name = "games.RES_DESK_ENTER"
	return {res_name, {
		table_id = table_id,
	}}
end

--离开
function DeskMessage:reqLeaveDesk(msg_data)
    local user_id = msg_data.user_id    
    local user = g.user_manager:getUser(user_id)
	local res_name = "games.RES_DESK_LEAVE"
    if not user then --玩家不在这个桌子里
    	g.log:error("_____调用离桌子，但玩家不在这个桌子里________", msg_data)
		return {res_name, {
			error_code = 1,
			error_msg = "离桌失败"
		}}
    end
    local res
    if g.desk.leaveDeskCallback then
    	res = g.desk:leaveDeskCallback(user_id)
    end	
	if res and next(res) then
		--站起,删除
		g.user_manager:removeUser(user_id)
		return res
	end
	--站起,删除
	g.user_manager:removeUser(user_id)

	return {res_name, {
		error_code = 0,
		error_msg = "离桌成功",
	}}
end

-- 得到桌子状态
function DeskMessage:deskStatusReq(msg_data)
    local user_id = msg_data.user_id    
    local user = g.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	g.log:error("_____调用deskStatusReq，但玩家不在这个桌子里________",msg_data)
		return {deskStatusRes, {
			error_code=1,
			error_msg="玩家不在这个桌子里",
		}}
    end
    local res
    if g.desk.deskStatusCallback then 
    	res = g.desk:deskStatusCallback(user_id)
    end		
	--桌子上人数各种共用信息
	if res and next(res) then 
		return res
	end
	return {deskStatusRes, {
		error_code = 1,
		error_msg = "取桌子状态错误",
	}}	

end

-- 请求坐下
function DeskMessage:reqSitDown(msg_data)
    local user_id = msg_data.user_id    
    local user = g.user_manager:getUser(user_id)
	local res_name = "games.RES_DESK_SITDOWN"
    if not user then --玩家不在这个桌子里
    	g.log:error("_____调用reqSitDown，但玩家不在这个桌子里________",msg_data)
		return {res_name, {
			error_code=1,
			error_msg="玩家不在这个桌子里"
		}}
    end
	local seat_id = g.user_manager:sitDown(user_id)
	if seat_id then 
		if g.desk.sitDownCallback then --让各个游戏做它要做的事
			g.desk:sitDownCallback()
		end
		return {res_name, {
			error_code = 0,
			seat_id = seat_id,
		}}
	end
	return {res_name, {
		error_code = 1,
		error_msg = "坐下错误",
	}}
end

-- 得到桌子状态
function DeskMessage:reqStandUp(msg_data)
    local user_id = msg_data.user_id    
    local user = g.user_manager:getUser(user_id)
	local res_name = "games.RES_DESK_SITUP"
    if not user then --玩家不在这个桌子里
    	g.log:error("_____调用reqStandUp，但玩家不在这个桌子里________",msg_data)
		return {res_name, {
			error_code = 1,
			error_msg = "玩家不在这个桌子里"
		}}
    end
	local seat_id = g.user_manager:standUp(user_id)
	if seat_id then 
		if g.desk.standUpCallback then --让各个游戏做它要做的事
			g.desk:standUpCallback()
		end
		return {res_name, {
			error_code = 0,
			seat_id = seat_id,
		}}
	end
	return {res_name, {
		error_code = 1,
		error_msg = "站起错误",
	}}	

end

--掉线了
function DeskMessage:disconnect(msg_data)
	print("__DeskMessage____disconnect___", msg_data)
	local user_id = msg_data.user_id
	local user = g.user_manager:getUser(user_id)
	if not user then --玩家不在这个桌子里
		error_log("_____调用disconnect，但玩家不在这个桌子里________",msg_data)
	end
	local res
	if g.desk.disconnectCallback then
		res = g.desk:disconnectCallback(user_id)
	end
	if res and next(res) then
		return res
	end
	--旁观者掉线马上踢出大厅
	--从user_manager中删除
	g.user_manager:removeUser(user_id)
	return {error_code=0}
end









--GM命令控制
function DeskMessage:cotrolReq(msg_data)
	-- self.machine:setGmCard(playerid,cards,repeatNum)
end

--更新属性
function DeskMessage:updatePropertyNt(playerid, prop_data)
	print("_________________玩家数据更新___",data)
	return true
end

--广播
function DeskMessage:broadcastNt( pack )
	return true
end

--安全关服
function DeskMessage:safeCloseReq(close)
	self.close = close
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------


return DeskMessage